package org.clickframes.plugins.jee6.factory;

import org.clickframes.model.Entity;
import org.clickframes.model.EntityProperty;
import org.clickframes.model.EntityPropertyType;
import org.clickframes.model.Field;
import org.clickframes.model.mvc.GeneralClass;
import org.clickframes.model.mvc.SimpleMethod;
/**
 * Prototype of using Java to ensure compile checking and make it easier to ensure no duplicate or unused imports.  
 * @author steven
 *
 */
public class JPA2EntityFactory {
	public static GeneralClass createEntity(Entity entity) {
		GeneralClass out = new GeneralClass(entity.getName());
		for(EntityProperty property : entity.getProperties()){
			
			//handle fields and imports.
			EntityPropertyType type = property.getType();
			String fieldType=null;
			if(type==EntityPropertyType.TEXT){
				fieldType = "String";
			}else if(type == EntityPropertyType.INT){
				fieldType = "int";
			}else if(type == EntityPropertyType.DATE){
				fieldType = "Date";
				out.getImports().add("java.util.Date");
			}else if(type == EntityPropertyType.BOOLEAN){
				fieldType = "boolean";
			}else if(type == EntityPropertyType.ENTITY){
				fieldType = property.getForeignEntity().getName();
			}else if(type == EntityPropertyType.FILE){
				fieldType = "FilePlaceholder";
			}else{
				throw new RuntimeException("I don't know what to do with" + type);
			}

			Field field = new Field(fieldType, property.getId());
			field.setComment(property.getDescription());
			out.getFields().add(field);
		}
		//generate getters first and then setters in order fields were declared.

		for(Field field : out.getFields()){
			out.getMethods().add(SimpleMethod.createGetter(field));
		}

		for(Field field : out.getFields()){
			out.getMethods().add(SimpleMethod.createSetter(field));
		}
		return out;
	}
}
